package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVO;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.annotation.Resource;

@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;  // 三级分类的service
    @Resource
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService relationService;

    /**
     * 获取属性分组的关联的所有属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    private R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        // 根据当前属性分组id获取他的多条属性(桥梁为他俩中间表)
        /**
         * 这个简单,前端要展示的数据都在pms_attr表里面
         */
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 查询属性分组没有关联的所有属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) { // params不仅有key,还有分页参数
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 获取当前分类下的所有属性分组,而且还要带上他所有的属性
     * 请求的地址:/product/attrgroup/{catelogId}/withattr
     * 请求的参数:catelogId
     * 响应的数据:好像是attrGroup实体类+attr实体类+attrGroupID字段
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        List<AttrGroupWithAttrsVO> vos = attrGroupService.getAttrGroupWithAttrsBycatelogId(catelogId);
        return R.ok().put("data",vos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);// 根据前端传来的attrGroupId来查属性分组信息
        // 根据attrGroupId在查到attrGroupEntity的时候,把attrGroup整个完整路径放进去
        Long catelogId = attrGroup.getCatelogId();// 从attrGroup中取出catelog_id,这样我们就拿到了分类id,接下来去查分类的完整路径就好了
        Long[] path = categoryService.findCatelogPath(catelogId);// 根据三级分类catelogId来查找这个id的完整路径,即他的二级分类id和一级分类id都给找出来
        attrGroup.setCatelogPath(path);// CatelogPath不是数据表中有的字段,所以需要@TableField(exist = false)
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 新增关联关系:在属性分组中,选择关联,选择新建关联触发的代码
     * 看开发文档:页面请求参数:attrGroupId:分组id   attrId:属性id  服务器响应参数: msg:"success",code:0
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVO> vos) {
        System.out.println("vos:" + vos);
        relationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    // 将属性分组关联的属性信息给移除掉

    /**
     * 这个其实有点麻烦了,最简单的做法自然是传个中间表的id,然后根据中间表id删除数据就可以了,然而他传的是attrId和attrGroupId
     * 那没办法了,想办法把这两个属性给合到中间表里,看实现类
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] vos) {
        /**
         * 打印的是vos:[AttrGroupRelationVO(attrId=7, attrGroupId=8)]
         * attrGroupId是属性分组   attrId是被该属性分组移除的那个属性(准确讲叫规格参数)
         */

        System.out.println("vos:" + Arrays.toString(vos));
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
